package com.cecw.Utils;

import com.cecw.dao.ProjectMapper;
import com.cecw.pojo.Project;
import com.cecw.service.ProjectService;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author linlingxin
 * @date 2024/5/24 15:25
 */
public class DataCrawlerUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(DataCrawlerUtil.class);


    public static void dataCraw(ProjectMapper projectMapper){
        // 拾贝云门户登录账号密码
        String username = "chenjinming";
        String password = "15626227887@2024";
        // sonarqube登录账号
        String problemUsername = "test";
        String problemPassword = "Aa123456789";
        // 获取token所需的信息
        String clientId = "cwos";
        String clientSecret = "password";
        // 获取token路径
        String tokenUrl = "https://devops.p.cyberwing.com/api/oauth/token";

        // bearerToken的动态获取
        String bearerToken = GetBearerToken.getToken(tokenUrl, clientId,clientSecret, username, password);

        // 项目ListUrl
        String proListUrl = "https://devops.p.cyberwing.com/cwapp/DoProductService/listProduct";

        // 直接写成List
        List<String[]> problemUrlList = new ArrayList<>();

        // 阳江抽水蓄能电站反恐达标安防
        problemUrlList.add("http://sonar.cwos.io/api/issues/search?componentKeys=com.jxtech.pct%3Apct-build&s=FILE_LINE&resolved=false&ps=100&facets=severities%2Ctypes&additionalFields=_all&timeZone=Asia%2FShanghai".split(","));
        // EventLog项目
        problemUrlList.add("http://sonar.cwos.io/api/issues/search?componentKeys=com.jxtech.gdeventlog%3Ahklogrecv&s=FILE_LINE&resolved=false&ps=100&facets=severities%2Ctypes&additionalFields=_all&timeZone=Asia%2FShanghai,http://sonar.cwos.io/api/issues/search?componentKeys=com.jxtech.gdeventlog%3Agxlogtransfer&s=FILE_LINE&resolved=false&ps=100&facets=severities%2Ctypes&additionalFields=_all&timeZone=Asia%2FShanghai".split(","));
        // 桥巩一体化后端
        problemUrlList.add("http://sonar.cwos.io/api/issues/search?componentKeys=com.jxtech.szgx-icp%3Aszgx-icp&s=FILE_LINE&resolved=false&ps=100&facets=severities%2Ctypes&additionalFields=_all&timeZone=Asia%2FShanghai".split(","));
        // 智慧基建
        problemUrlList.add("http://sonar.cwos.io/api/issues/search?componentKeys=com.jxtech.pie%3Apie-build&s=FILE_LINE&resolved=false&ps=100&facets=severities%2Ctypes&additionalFields=_all&timeZone=Asia%2FShanghai".split(","));
        // 储能集控中心项目
        problemUrlList.add("http://sonar.cwos.io/api/issues/search?componentKeys=com.jxtech.bop%3Abop-parent&s=FILE_LINE&resolved=false&ps=100&facets=severities%2Ctypes&additionalFields=_all&timeZone=Asia%2FShanghai".split(","));
        // 智慧安防
        problemUrlList.add("http://sonar.cwos.io/api/issues/search?componentKeys=com.jxtech.ggscgk%3Aggscgk-build&s=FILE_LINE&resolved=false&ps=100&facets=severities%2Ctypes&additionalFields=_all&timeZone=Asia%2FShanghai".split(","));
        // 海域预警后台（什么都未执行）
        problemUrlList.add("".split(","));
        // 修试公司2023-2025年抽水蓄能
        //电站设备全景数据分析评估及检修决策辅助系统研发项目
        problemUrlList.add("http://sonar.cwos.io/api/issues/search?componentKeys=com.jxtech.iac%3Aiac-build&s=FILE_LINE&resolved=false&ps=100&facets=severities%2Ctypes&additionalFields=_all&timeZone=Asia%2FShanghai".split(","));
        // CN数据湖建设后端代码质量检查
        problemUrlList.add("http://sonar.cwos.io/api/issues/search?componentKeys=com.jx%3Adatalake&s=FILE_LINE&resolved=false&ps=100&facets=severities%2Ctypes&additionalFields=_all&timeZone=Asia%2FShanghai".split(","));
        // 智慧设备
        problemUrlList.add("http://sonar.cwos.io/api/issues/search?componentKeys=com.jxtech.iac%3Azhsb-build&s=FILE_LINE&resolved=false&ps=100&facets=severities%2Ctypes&additionalFields=_all&timeZone=Asia%2FShanghai".split(","));
        // 应用产品 | 生产运营管控
        problemUrlList.add("http://sonar.cwos.io/api/issues/search?componentKeys=com.jxtech.ggscgk%3Aggscgk&s=FILE_LINE&resolved=false&ps=100&facets=severities%2Ctypes&additionalFields=_all&timeZone=Asia%2FShanghai,http://sonar.cwos.io/api/issues/search?componentKeys=com.jxtech.ggscgk%3Aggscgk&s=FILE_LINE&resolved=false&ps=100&facets=severities%2Ctypes&additionalFields=_all&timeZone=Asia%2FShanghai".split(","));

        // 项目ListUrl
        List<String> projectUrlList = new ArrayList<>();
        // 阳江抽水蓄能电站反恐达标安防
        projectUrlList.add("https://devops.p.cyberwing.com/cwapp/DoServiceQualityService/listQualityData?_=1716179217911&page=1&rows=20&productId=1713148620028021&serviceName=&orderField=&isAsc=false&testUrl=");
        // EventLog项目
        projectUrlList.add("https://devops.p.cyberwing.com/cwapp/DoServiceQualityService/listQualityData?_=1716181124817&page=1&rows=20&productId=1712655436548881&serviceName=&orderField=&isAsc=false&testUrl=");
        // 桥巩一体化后端
        projectUrlList.add("https://devops.p.cyberwing.com/cwapp/DoServiceQualityService/listQualityData?_=1716181383839&page=1&rows=20&productId=1712487587057442&serviceName=&orderField=&isAsc=false&testUrl=");
        // 智慧基建
        projectUrlList.add("https://devops.p.cyberwing.com/cwapp/DoServiceQualityService/listQualityData?_=1716183654634&page=1&rows=20&productId=1712109872779911&serviceName=&orderField=&isAsc=false&testUrl=");
        // 储能集控中心项目
        projectUrlList.add("https://devops.p.cyberwing.com/cwapp/DoServiceQualityService/listQualityData?_=1716183738902&page=1&rows=20&productId=1711702054438721&serviceName=&orderField=&isAsc=false&testUrl=");
        // 智慧安防
        projectUrlList.add("https://devops.p.cyberwing.com/cwapp/DoServiceQualityService/listQualityData?_=1716183974602&page=1&rows=20&productId=1711453771649489&serviceName=&orderField=&isAsc=false&testUrl=");
        // 海域预警后台
        projectUrlList.add("https://devops.p.cyberwing.com/cwapp/DoServiceQualityService/listQualityData?_=1716184041969&page=1&rows=20&productId=1711357414944791&serviceName=&orderField=&isAsc=false&testUrl=");
        // 修试公司2023-2025年抽水蓄能
        //电站设备全景数据分析评估及检修决策辅助系统研发项目
        projectUrlList.add("https://devops.p.cyberwing.com/cwapp/DoServiceQualityService/listQualityData?_=1716188770451&page=1&rows=20&productId=1711356804209555&serviceName=&orderField=&isAsc=false&testUrl=");
        // CN数据湖建设后端代码质量检查
        projectUrlList.add("https://devops.p.cyberwing.com/cwapp/DoServiceQualityService/listQualityData?_=1716184628104&page=1&rows=20&productId=1711356282929504&serviceName=&orderField=&isAsc=false&testUrl=");
        // 智慧设备
        projectUrlList.add("https://devops.p.cyberwing.com/cwapp/DoServiceQualityService/listQualityData?_=1716184750213&page=1&rows=20&productId=1711268660265556&serviceName=&orderField=&isAsc=false&testUrl=");
        // 应用产品 | 生产运营管控
        projectUrlList.add("https://devops.p.cyberwing.com/cwapp/DoServiceQualityService/listQualityData?_=1716184817337&page=1&rows=20&productId=1703577254895803&serviceName=&orderField=&isAsc=false&testUrl=");


        List<Project> proList = new ArrayList<>();
        for (int i = 0; i < projectUrlList.size(); i++) {
            Project project = new Project();
            /**
             * 获取项目页面的相关信息(这里就会排除待执行和关闭的项目，拿到就只有开启了的项目)
             * 在这个工具类里面可以得到status、productCode、serviceName、projectKey、nCloc、createTime
             */
            GetInfoUtil1.getInfo(project, projectUrlList.get(i), bearerToken);

            // 获取问题页面的相关信息
            String[] problemUrlArr = problemUrlList.get(i);
            // 如果有多个子项目，但是都没有启动的话，problemUrlArr不会大于1，因为拿不到问题Url
            if (problemUrlArr.length > 1) {
                // 有多个子项目并且启动的不止一个
                for (int i1 = 0; i1 < problemUrlArr.length; i1++) {
                    /**
                     * 在这个工具类里面可以得到MAJOR、MINOR、CRITICAL、BLOCKER、INFO、CODE_SMELL、BUG、VULNERABILITY
                     */
                    GetInfoUtil.getInfo(project, problemUrlArr[i1], problemUsername, problemPassword);
                }
            } else {
                // 没启动的项目
                if (problemUrlArr[0].equals("")) {
                    project.setStatus("PENDING");
                    proList.add(project);
                    continue;
                }
                // 启动了的项目
                GetInfoUtil.getInfo(project, problemUrlArr[0], problemUsername, problemPassword);
            }
            CalScoreUtil.calScore(project);
            proList.add(project);
        }

        // 获取项目名称和id(因为有的serviceName对不上项目名称)
        GetInfoUtil2.getInfo(proList, proListUrl, bearerToken);


        // 输出没问题
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(System.currentTimeMillis());
        System.out.println(simpleDateFormat.format(date)+"的数据如下：");
        for (Project project : proList) {
            System.out.println(project.toString());
        }

        // 写数据库
        LOGGER.info("开始写入数据库");
        projectMapper.insertBatch(proList);
        LOGGER.info("成功写入数据库");
    }
}
